package com.ruoyi.binance.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.domain.*;
import com.ruoyi.binance.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.binance.service.IBinancePriceMonitoringService;

/**
 * 日均价监控Service业务层处理
 * 
 * @author zq
 * @date 2025-08-05
 */
@Service
public class BinancePriceMonitoringServiceImpl implements IBinancePriceMonitoringService 
{
    @Autowired
    private BinancePriceMonitoringMapper binancePriceMonitoringMapper;
    @Autowired
    private BinanceMinKMapper binanceMinKMapper;
    @Autowired
    private BinanceMinAverageMapper binanceMinAverageMapper;
    @Autowired
    private Binance15minAverageMapper binance15minAverageMapper;
    @Autowired
    private Binance30minAverageMapper binance30minAverageMapper;
    @Autowired
    private BinanceHourAverageMapper binanceHourAverageMapper;
    @Autowired
    private Binance15minDayAveMapper binance15minDayAveMapper;
    @Autowired
    private Binance15macdMapper binance15macdMapper;

    /**
     * 查询日均价监控
     * 
     * @param code 日均价监控主键
     * @return 日均价监控
     */
    @Override
    public BinancePriceMonitoring selectBinancePriceMonitoringByCode(String code)
    {
        return binancePriceMonitoringMapper.selectBinancePriceMonitoringByCode(code);
    }

    /**
     * 查询日均价监控列表
     * 
     * @param binancePriceMonitoring 日均价监控
     * @return 日均价监控
     */
    @Override
    public List<BinancePriceMonitoring> selectBinancePriceMonitoringList(BinancePriceMonitoring binancePriceMonitoring)
    {
        return binancePriceMonitoringMapper.selectBinancePriceMonitoringList(binancePriceMonitoring);
    }

    /**
     * 新增日均价监控
     * 
     * @param binancePriceMonitoring 日均价监控
     * @return 结果
     */
    @Override
    public int insertBinancePriceMonitoring(BinancePriceMonitoring binancePriceMonitoring)
    {
        binancePriceMonitoring.setCreateTime(DateUtils.getNowDate());
        return binancePriceMonitoringMapper.insertBinancePriceMonitoring(binancePriceMonitoring);
    }

    /**
     * 修改日均价监控
     * 
     * @param binancePriceMonitoring 日均价监控
     * @return 结果
     */
    @Override
    public int updateBinancePriceMonitoring(BinancePriceMonitoring binancePriceMonitoring)
    {
        binancePriceMonitoring.setUpdateTime(DateUtils.getNowDate());
        return binancePriceMonitoringMapper.updateBinancePriceMonitoring(binancePriceMonitoring);
    }

    /**
     * 批量删除日均价监控
     * 
     * @param codes 需要删除的日均价监控主键
     * @return 结果
     */
    @Override
    public int deleteBinancePriceMonitoringByCodes(String[] codes)
    {
        return binancePriceMonitoringMapper.deleteBinancePriceMonitoringByCodes(codes);
    }

    /**
     * 删除日均价监控信息
     * 
     * @param code 日均价监控主键
     * @return 结果
     */
    @Override
    public int deleteBinancePriceMonitoringByCode(String code)
    {
        return binancePriceMonitoringMapper.deleteBinancePriceMonitoringByCode(code);
    }

    @Override
    public JSONArray chart(String code, String startTime, String endTime) {
        List<BinanceMinK> stockMinKS = binanceMinKMapper.selectStockPriceMonitoringByTime(code, startTime, endTime);
        List<BinanceMinAverage> stockMinAverages = binanceMinAverageMapper.selectStockMinAverageTime(code, startTime, endTime);
        List<Binance15minAverage> binance15minAverages = binance15minAverageMapper.select15minAverageByTime(code, startTime, endTime);
        List<Binance30minAverage> binance30minAverages = binance30minAverageMapper.select30minAverageByTime(code, startTime, endTime);
        List<BinanceHourAverage> binanceHourAverages = binanceHourAverageMapper.selectHourAverageByTime(code, startTime, endTime);
        List<Binance15minDayAve> binance15minDayAves = binance15minDayAveMapper.select15minDayAveByTime(code, startTime, endTime);
        //将stockMinAverages转为map,key为时间，value为价格
        Map<Date, BigDecimal> stockMinAverageMap = stockMinAverages.stream().collect(Collectors.toMap(BinanceMinAverage::getTime, BinanceMinAverage::getPrice));
        Map<Date, BigDecimal> binance15minAverageMap = binance15minAverages.stream().collect(Collectors.toMap(Binance15minAverage::getTime, Binance15minAverage::getPrice));
        Map<Date, BigDecimal> binance30minAverageMap = binance30minAverages.stream().collect(Collectors.toMap(Binance30minAverage::getTime, Binance30minAverage::getPrice));
        Map<Date, BigDecimal> binanceHourAverageMap = binanceHourAverages.stream().collect(Collectors.toMap(BinanceHourAverage::getTime, BinanceHourAverage::getPrice));
        Map<Date, BigDecimal> binance15minDayAveMap = binance15minDayAves.stream().collect(Collectors.toMap(Binance15minDayAve::getTime, Binance15minDayAve::getPrice));

        JSONArray objects = new JSONArray();
        for (BinanceMinK stockMinK : stockMinKS){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("time", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, stockMinK.getTime()));
            jsonObject.put("price", stockMinAverageMap.get(stockMinK.getTime()));
            jsonObject.put("min15", binance15minAverageMap.get(stockMinK.getTime()));
            jsonObject.put("min30", binance30minAverageMap.get(stockMinK.getTime()));
            jsonObject.put("hour", binanceHourAverageMap.get(stockMinK.getTime()));
            jsonObject.put("min15_day", binance15minDayAveMap.get(stockMinK.getTime()));
            jsonObject.put("close", stockMinK.getClose());
            objects.add(jsonObject);
        }
        return objects;
    }

    @Override
    public JSONArray macd15(String code, String startTime, String endTime) {
        List<Binance15macd> binance15macds = binance15macdMapper.selectBinance15macdByTime(code, startTime, endTime);
        JSONArray objects = new JSONArray();
        for (Binance15macd macd : binance15macds){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("time", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, macd.getTime()));
            jsonObject.put("bar", macd.getBar());
            objects.add(jsonObject);
        }
        return objects;
    }
}
